main: Also support CLI extensions in `/usr/libexec/libostree/ext`
authorColin Walters <walters@verbum.org>
Tue, 11 Jan 2022 21:46:49 +0000 (16:46 -0500)
committerColin Walters <walters@verbum.org>
Wed, 12 Jan 2022 01:13:33 +0000 (20:13 -0500)
In fixing https://github.com/coreos/rpm-ostree/pull/3323
I felt that it was a bit ugly we're installing `/usr/bin/ostree-container`.

It's kind of an implementation detail.  We want users to use
`ostree container`.

Let's support values outside of $PATH too.

For example, this also ensures that TAB completion for `ost` expands
to `ostree ` with a space.

configure.ac
src/libostree/ostree-1.pc.in
src/ostree/ot-main.c
tests/kolainst/destructive/basic-misc.sh [new file with mode: 0644]
tests/test-cli-extensions.sh

index 879fe97faa9db213faebd095ac02e81d7943d56c..763ffb04877ecc6a1c5583244764270a4487d61a 100644 (file)
@@ -25,6 +25,10 @@ AC_SUBST([YEAR_VERSION], [year_version])
 AC_SUBST([RELEASE_VERSION], [release_version])
 AC_SUBST([PACKAGE_VERSION], [package_version])
 
+dnl automake variables we want in pkg-config
+pkglibexecdir=$libexecdir/$PACKAGE
+AC_SUBST(pkglibexecdir)
+
 AS_IF([echo "$CFLAGS" | grep -q -E -e '-Werror($| )'], [], [
 CC_CHECK_FLAGS_APPEND([WARN_CFLAGS], [CFLAGS], [\
   -pipe \
index 9a4debcee719593698531b083645a6ea5767336e..cbf0a13c98e71aa01ec1a6f4e3ea5c5d0dd30fa4 100644 (file)
@@ -3,6 +3,7 @@ exec_prefix=@exec_prefix@
 libdir=@libdir@
 includedir=@includedir@
 features=@OSTREE_FEATURES@
+cliextdir=@pkglibexecdir@/ext
 
 Name: OSTree
 Description: Git for operating system binaries
index b72fa9d42d5e759091efdd847dd6e7f50a27ca01..7a4c470787c3e5f49c7452c04e17032977296bf7 100644 (file)
@@ -41,6 +41,10 @@ static gboolean opt_verbose;
 static gboolean opt_version;
 static gboolean opt_print_current_dir;
 
+// TODO: make this public?  But no one sane wants to use our C headers
+// to find where to put files.  Maybe we can make it printed by the CLI?
+#define _OSTREE_EXT_DIR PKGLIBEXECDIR "/ext"
+
 static GOptionEntry global_entries[] = {
   { "verbose", 'v', 0, G_OPTION_ARG_NONE, &opt_verbose, "Print debug information during command processing", NULL },
   { "version", 0, 0, G_OPTION_ARG_NONE, &opt_version, "Print version information and exit", NULL },
@@ -188,7 +192,7 @@ ostree_command_lookup_external (int argc,
 
   // Find the first verb (ignoring all earlier flags), then
   // check if it is a known native command. Otherwise, try to look it
-  // up in $PATH.
+  // up in /usr/lib/ostree/ostree-$cmd or $PATH.
   // We ignore argv[0] here, the ostree binary itself is not multicall.
   for (guint arg_index = 1; arg_index < argc; arg_index++)
     {
@@ -204,10 +208,18 @@ ostree_command_lookup_external (int argc,
             return NULL;
         }
 
+
       g_autofree gchar *ext_command = g_strdup_printf ("ostree-%s", current_arg);
+
+      /* First, search in our libdir /usr/lib/ostree/ostree-$cmd */
+      g_autofree char *ext_lib = g_strconcat (_OSTREE_EXT_DIR, "/", ext_command, NULL);
+      struct stat stbuf;
+      if (stat (ext_lib, &stbuf) == 0)
+        return g_steal_pointer (&ext_lib);
+
+      /* Otherwise, look in $PATH */
       if (g_find_program_in_path (ext_command) == NULL)
           return NULL;
-
       return g_steal_pointer (&ext_command);
     }
 
diff --git a/tests/kolainst/destructive/basic-misc.sh b/tests/kolainst/destructive/basic-misc.sh
new file mode 100644 (file)
index 0000000..6d14cc0
--- /dev/null
@@ -0,0 +1,23 @@
+#!/bin/bash
+
+# Random misc tests
+
+set -xeuo pipefail
+
+. ${KOLA_EXT_DATA}/libinsttest.sh
+
+echo "1..1"
+date
+
+# Test CLI extensions installed alongside the system
+extdir=/usr/libexec/libostree/ext/
+mkdir -p "${extdir}"
+ln -sr /usr/bin/env ${extdir}/ostree-env
+
+env TESTENV=foo ostree env > out.txt
+assert_file_has_content out.text TESTENV=foo
+rm -vf "${extdir}/ostree-env"
+echo "ok env"
+
+# End test
+date
index 1fe9c037b447e90a89fcc58efde1b210a634e4ea..e1916036e0fc726086007b38d73da59ae4b19737 100755 (executable)
@@ -9,6 +9,9 @@ set -euo pipefail
 
 echo '1..2'
 
+# Test CLI extensions via $PATH.  If you change this, you may
+# also want to change the corresponding destructive version in
+# tests/kolainst/destructive/basic-misc.sh
 mkdir -p ./localbin
 ORIG_PATH="${PATH}"
 export PATH="./localbin/:${PATH}"